package com.chinawlx.wlxteacher.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.bean.WLXTimeLineBean;
import com.chinawlx.wlxteacher.ui.activity.WLXPhotoViewActivity;
import com.chinawlx.wlxteacher.ui.activity.WLXPlayVideoActivity;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.wlx_grade_timeline;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者:吴名
 * 时间:16/8/16
 * 描述:时间线的ExpandListView的适配器
 */
public class WLXTimeLineListViewAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<Long> dateList;
    private Map<Long, ArrayList<wlx_grade_timeline>> map;

    public WLXTimeLineListViewAdapter(Context context, List<Long> dateList, Map<Long,
            ArrayList<wlx_grade_timeline>> map) {
        this.mContext = context;
        this.dateList = dateList;
        this.map = map;
    }

    @Override
    public int getChildTypeCount() {
        return 5;
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        String typeCode = map.get(dateList.get(groupPosition)).get(childPosition).getType_code();
        if (typeCode.equals("homework")) {
            return 1;
        } else if (typeCode.equals("medal")) {
            return 2;
        } else if (typeCode.equals("result")) {
            return 3;
        } else if (typeCode.equals("schedule")) {
            return 4;
        } else {
            return 0;
        }
    }

    @Override
    public int getGroupCount() {
        return dateList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return map.get(dateList.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        View view = convertView;
        GroupViewHolder groupViewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_el_main_timeline_group,
                    null);
            groupViewHolder = new GroupViewHolder(view);
        } else {
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        //显示月日
        Date date = new Date(dateList.get(groupPosition) * 60 * 60 * 24 * 1000);
        groupViewHolder.tvMainTimelineItemGroupMonth.setText(date.getMonth() + 1 + "月");
        groupViewHolder.tvMainTimelineItemGroupDay.setText(date.getDate() + "");
        return view;
    }

    /**
     * 分组的ViewHolder(月日)
     */
    static class GroupViewHolder {
        @BindView(R.id.tv_main_timeline_item_group_day)
        TextView tvMainTimelineItemGroupDay;
        @BindView(R.id.tv_main_timeline_item_group_month)
        TextView tvMainTimelineItemGroupMonth;

        public GroupViewHolder(View view) {
            view.setTag(this);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        View view = convertView;
        HomeworkViewHolder homeworkViewHolder = null;
        MedalViewHolder medalViewHolder = null;
        ResultViewHolder resultViewHolder = null;
        ClasscontentViewHolder classcontentViewHolder = null;
        DynamicViewHolder dynamicViewHolder = null;
        int childType = getChildType(groupPosition, childPosition);
        final wlx_grade_timeline timeLine = map.get(dateList.get(groupPosition)).get(childPosition);
        if (view == null) {
            if (childType == 1) {//homework
                view = LayoutInflater.from(mContext).inflate(R.layout
                        .item_main_timeline_homework, null);
                homeworkViewHolder = new HomeworkViewHolder(view);
            } else if (childType == 2) {//medal
                view = LayoutInflater.from(mContext).inflate(R.layout.item_main_timeline_medal,
                        null);
                medalViewHolder = new MedalViewHolder(view);
            } else if (childType == 3) {//result
                view = LayoutInflater.from(mContext).inflate(R.layout.item_main_timeline_result,
                        null);
                resultViewHolder = new ResultViewHolder(view);
            } else if (childType == 4) {//schedule
                view = LayoutInflater.from(mContext).inflate(R.layout
                        .item_main_timeline_classcontent, null);
                classcontentViewHolder = new ClasscontentViewHolder(view);
            } else {//班级动态
                view = LayoutInflater.from(mContext).inflate(R.layout.item_main_timeline_dynamic,
                        null);
                dynamicViewHolder = new DynamicViewHolder(view);
            }
        } else {
            if (childType == 1) {//homework
                homeworkViewHolder = (HomeworkViewHolder) view.getTag();
            } else if (childType == 2) {//medal
                medalViewHolder = (MedalViewHolder) view.getTag();
            } else if (childType == 3) {//result
                resultViewHolder = (ResultViewHolder) view.getTag();
            } else if (childType == 4) {//schedule(课程内容)
                classcontentViewHolder = (ClasscontentViewHolder) view.getTag();
            } else {//班级动态
                dynamicViewHolder = (DynamicViewHolder) view.getTag();
            }
        }

        WLXTimeLineBean.Data.TimelineList.Extend extend = new Gson().fromJson(timeLine
                .getExtend_info(), new TypeToken<WLXTimeLineBean.Data.TimelineList.Extend>() {
        }.getType());
        final ArrayList<String> resource_url = (ArrayList<String>) extend.getResource_url();
        if (childType == 1) {//homework
            //======显示图片
            final boolean isVideoResourxe = extend.getResource_code().equals("video");
            if (isVideoResourxe) {
                homeworkViewHolder.ivVideo.setVisibility(View.VISIBLE);
            } else {
                homeworkViewHolder.ivVideo.setVisibility(View.GONE);
            }
            if (resource_url.size() > 0) {
                String resourceUrl = resource_url.get(0);
                //视频作业,改视频地址为图片地址
                if (isVideoResourxe) {
                    resourceUrl = resourceUrl.replace(".mp4", ".png");
                }
                Glide.with(mContext).load(resourceUrl).placeholder(R.drawable.place_holder)
                        .centerCrop().into(homeworkViewHolder.ivMainTimelineHomework);

                //======展示图片或者视频
                homeworkViewHolder.ivMainTimelineHomework.setOnClickListener(new View
                        .OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //展示图片或者视频
                        Intent intent;
                        if (isVideoResourxe){
                            intent = new Intent(mContext, WLXPlayVideoActivity.class);
                            intent.putExtra(WLXPlayVideoActivity.VIDEO_URL, resource_url.get(0));
                        }else {
                            intent = new Intent(mContext,WLXPhotoViewActivity.class);
                            intent.putExtra(WLXPhotoViewActivity.URLS, resource_url);
                        }
                        mContext.startActivity(intent);
                    }
                });

            } else {
                WLXLogUtil.printE("该作业无地址");
                Glide.with(mContext).load("").placeholder(R.drawable.place_holder).centerCrop()
                        .into(homeworkViewHolder.ivMainTimelineHomework);
            }
            //======显示是否点评
            if (extend.isIs_comment()) {
                homeworkViewHolder.ivMainTimelineHomeworkReviewed.setVisibility(View.VISIBLE);
            } else {
                homeworkViewHolder.ivMainTimelineHomeworkReviewed.setVisibility(View.GONE);
            }

        } else if (childType == 2) {//medal
            medalViewHolder.tvMainTimelineItemMedalCount.setText(timeLine.getContent());

            String medalCode = extend.getMedal_code();
            switch (medalCode){
                case "medal_31":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .growth_star1);
                    break;
                case "medal_32":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .growth_star2);
                    break;
                case "medal_33":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .growth_star3);
                    break;
                case "medal_34":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .growth_star4);
                    break;
                case "medal_35":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .growth_star5);
                    break;
                case "medal_41":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .wisdom_star1);
                    break;
                case "medal_42":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .wisdom_star2);
                    break;
                case "medal_43":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .wisdom_star3);
                    break;
                case "medal_44":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .wisdom_star4);
                    break;
                case "medal_45":
                    medalViewHolder.ivMainTimelineItemMedalLevel.setImageResource(R.drawable
                            .wisdom_star5);
                    break;
            }



        } else if (childType == 3) {
            //==============result 成果展示
            if (resource_url.size() > 0) {
                Glide.with(mContext).load(resource_url.get(0)).placeholder(R.drawable
                        .place_holder).centerCrop().into(resultViewHolder.ivMainTimelineItemResult);
                resultViewHolder.ivMainTimelineItemResult.setOnClickListener(new View
                        .OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO: 16/9/18 展示图片或者视频
//                        Intent intent = new Intent(mContext, WLXMonthAchievementsActivity.class);
//                        intent.putExtra("content", timeLine.getContent());
//                        intent.putExtra("extend_info", timeLine.getExtend_info());
//                        mContext.startActivity(intent);
                    }
                });
            } else {
                Glide.with(mContext).load("").placeholder(R.drawable.place_holder).centerCrop()
                        .into(resultViewHolder.ivMainTimelineItemResult);
            }
        } else if (childType == 4) {//schedule(课程内容)
            classcontentViewHolder.tvMainTimelineItemClasscontent.setText(timeLine.getContent());
            if (extend.isIs_comment()) {
                classcontentViewHolder.btnMainTimelineItemEvaluate.setVisibility(View.GONE);
                // TODO: 16/8/18 设置评价的星星等级
                classcontentViewHolder.tvMainTimelineItemEvaluated.setVisibility(View.VISIBLE);
            } else {
                classcontentViewHolder.tvMainTimelineItemEvaluated.setVisibility(View.GONE);
                // TODO: 16/8/18 设置评价按钮的点击事件
                classcontentViewHolder.btnMainTimelineItemEvaluate.setVisibility(View.VISIBLE);
            }
        } else {//班级动态
            dynamicViewHolder.tvMainTimelineItemClasscontent.setText(timeLine.getContent());
            if (resource_url.size() >= 3) {
                Glide.with(mContext).load(resource_url.get(0)).placeholder(R.drawable
                        .place_holder).centerCrop().into(dynamicViewHolder.ivMainTimelineDynamic1);
                Glide.with(mContext).load(resource_url.get(1)).placeholder(R.drawable
                        .place_holder).centerCrop().into(dynamicViewHolder.ivMainTimelineDynamic2);
                Glide.with(mContext).load(resource_url.get(2)).placeholder(R.drawable
                        .place_holder).centerCrop().into(dynamicViewHolder.ivMainTimelineDynamic3);
            } else if (resource_url.size() == 2) {
                Glide.with(mContext).load(resource_url.get(0)).placeholder(R.drawable
                        .place_holder).centerCrop().into(dynamicViewHolder.ivMainTimelineDynamic1);
                Glide.with(mContext).load(resource_url.get(1)).placeholder(R.drawable
                        .place_holder).centerCrop().into(dynamicViewHolder.ivMainTimelineDynamic2);
            } else if (resource_url.size() == 1) {
                Glide.with(mContext).load(resource_url.get(0)).placeholder(R.drawable
                        .place_holder).centerCrop().into(dynamicViewHolder.ivMainTimelineDynamic1);
            } else {
                dynamicViewHolder.ivMainTimelineDynamic1.setVisibility(View.GONE);
                dynamicViewHolder.ivMainTimelineDynamic2.setVisibility(View.GONE);
                dynamicViewHolder.ivMainTimelineDynamic3.setVisibility(View.GONE);
            }
        }

        return view;
    }

    /**
     * 子item的点击事件是否生效
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    static class HomeworkViewHolder {
        @BindView(R.id.iv_main_timeline_homework)
        ImageView ivMainTimelineHomework;
        @BindView(R.id.iv_main_timeline_homework_reviewed)
        ImageView ivMainTimelineHomeworkReviewed;

        @BindView(R.id.iv_video)
        ImageView ivVideo;

        HomeworkViewHolder(View view) {
            view.setTag(this);
            ButterKnife.bind(this, view);
        }
    }

    static class MedalViewHolder {
        @BindView(R.id.tv_main_timeline_item_medal_count)
        TextView tvMainTimelineItemMedalCount;
        @BindView(R.id.iv_main_timeline_item_medal_level)
        ImageView ivMainTimelineItemMedalLevel;

        MedalViewHolder(View view) {
            view.setTag(this);
            ButterKnife.bind(this, view);
        }
    }

    static class ResultViewHolder {
        @BindView(R.id.iv_main_timeline_item_result)
        ImageView ivMainTimelineItemResult;

        ResultViewHolder(View view) {
            view.setTag(this);
            ButterKnife.bind(this, view);
        }
    }

    static class ClasscontentViewHolder {
        @BindView(R.id.tv_main_timeline_item_classcontent)
        TextView tvMainTimelineItemClasscontent;
        @BindView(R.id.tv_main_timeline_item_evaluated)
        TextView tvMainTimelineItemEvaluated;
        @BindView(R.id.btn_main_timeline_item_evaluate)
        Button btnMainTimelineItemEvaluate;

        ClasscontentViewHolder(View view) {
            view.setTag(this);
            ButterKnife.bind(this, view);
        }
    }

    static class DynamicViewHolder {
        @BindView(R.id.tv_main_timeline_item_classcontent)
        TextView tvMainTimelineItemClasscontent;
        @BindView(R.id.iv_main_timeline_dynamic1)
        ImageView ivMainTimelineDynamic1;
        @BindView(R.id.iv_main_timeline_dynamic2)
        ImageView ivMainTimelineDynamic2;
        @BindView(R.id.iv_main_timeline_dynamic3)
        ImageView ivMainTimelineDynamic3;

        DynamicViewHolder(View view) {
            view.setTag(this);
            ButterKnife.bind(this, view);
        }
    }
}
